package graphics;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.Vector;

import core.mouse.Clickable;
import core.mouse.OBCMouseListener;

public class GTestFrame implements Clickable{

	public static GTestFrame me;	
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	
	private Vector<ImageButton> buttons = new Vector<ImageButton>();
	private String shownImage = "ImageNotFound.png";
	private Color imageBgColor = Color.white;
	private int colorPickHeight=0;
	private int zoomDivider = 500;
	
	int height = 300;
	int sleepSize = 30;
	boolean sleeping = true;
	
	private Vector<Particle> testParticles = new Vector<Particle>();
	
	
	private static Rectangle bounds = new Rectangle(50, 50, 50, 50);
	private static Rectangle colorPickBounds = new Rectangle(275, 0, 25, 300);
	private static Rectangle zoomBounds = new Rectangle(300, 0, 25, 300);
	
	private GTestFrame(){
		OBCMouseListener.add(this);
		setSleeping(false);
		initButtons();
		setSleeping(true);
	}
	
	public static GTestFrame getInstance(){
		if(me == null)
			me = new GTestFrame();
		return me;
	}
	
	private void initButtons(){
		int buttonSize = 20;
		int x = 0;
		int y = screenSize.height-height;
		for(String s : ImageHandler.getInstance().getImageNames()){
			buttons.add(new ImageButton(s, new Rectangle(x, y, buttonSize, buttonSize)));
			y+=buttonSize;
			if(y > screenSize.height-buttonSize){
				y = screenSize.height-height;
				x += buttonSize;
			}
		}
		
	}
	
	private void setSleeping(boolean s){
		if(s){
			bounds = new Rectangle(screenSize.width-sleepSize, screenSize.height-sleepSize, sleepSize, sleepSize);
			sleeping = true;
		} 
		else{
			bounds = new Rectangle(0, screenSize.height-height, screenSize.width, height);
			sleeping = false;
			colorPickBounds=new Rectangle(275, screenSize.height-height, 25, height);
			zoomBounds = new Rectangle(300, screenSize.height-height, 25, height);
		}
	}
	
	@Override
	public void click(int x, int y) {
		if(sleeping){
			setSleeping(false);
		}
		else{
			for(ImageButton b : buttons){
				if(b.getBounds().contains(x, y)){
					shownImage = b.imageName;
				}
			}
			if(colorPickBounds.contains(x, y)){
				imageBgColor = colorFunc(y);
				colorPickHeight = y;
			}
			if(zoomBounds.contains(x, y)){
				zoomDivider = zoomDivider + y - (zoomBounds.y+zoomBounds.height/2);
				if(zoomDivider == 0)
					zoomDivider = 10;
			}
			
			
			if(x>screenSize.width-50 && y>screenSize.height-50)
				setSleeping(true);
		}
		
	}

	@Override
	public Rectangle getBounds() {
		return bounds;
	}

	@Override
	public void paint(Graphics g) {
		g.setColor(Color.lightGray);
		
		if(sleeping){
			
		}
		else{
			g.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
			for(ImageButton b : buttons){
				b.paint(g);
			}
			g.setColor(imageBgColor);
			g.fillRect(bounds.x+100, bounds.y, 175, 300);
			g.setColor(Color.black);
			g.drawString(shownImage, bounds.x+100, bounds.y+20);
			ImageHandler.getInstance().drawImage(bounds.x+100, bounds.y+25, 175, 175, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+100, bounds.y+200, 100, 100, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+200, bounds.y+200, 50, 50, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+200, bounds.y+250, 25, 25, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+200, bounds.y+275, 20, 20, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+225, bounds.y+250, 15, 15, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+250, bounds.y+200, 10, 10, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+250+1, bounds.y+211, 8, 8, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+250+2, bounds.y+222, 6, 6, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+250+3, bounds.y+233, 4, 4, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+250+4, bounds.y+244, 2, 2, shownImage, g);
			ImageHandler.getInstance().drawImage(bounds.x+250+4, bounds.y+234, 1, 1, shownImage, g);
			
			for(int y=colorPickBounds.y; y<=colorPickBounds.height+colorPickBounds.y; y++){
				g.setColor(colorFunc(y));
				if(y == colorPickHeight){
					g.setColor(Color.black);
				}
				g.drawLine(colorPickBounds.x, y, colorPickBounds.x+colorPickBounds.width, y);
				
			}
			g.setColor(Color.black);
			g.fillRect(zoomBounds.x, zoomBounds.y, zoomBounds.width, zoomBounds.height);
			g.setColor(Color.white);
			g.drawLine(zoomBounds.x, zoomBounds.y+zoomBounds.height/2, zoomBounds.x+zoomBounds.width, zoomBounds.y+zoomBounds.height/2);
			g.drawString("Zoom", zoomBounds.x+1, zoomBounds.y+10);
			g.drawString(""+zoomDivider, zoomBounds.x+1, zoomBounds.y+20);
			
			paintParticleEffectWindow(bounds.x+400, bounds.y, g);
			
			
		}
		
		g.setColor(Color.gray);
		g.fillArc(screenSize.width-30, screenSize.height-30, 60, 60, 90, 90);
		
		
	}
	
	private void paintParticleEffectWindow(int x, int y, Graphics g){
		g.setColor(imageBgColor);
		g.fillRect(x, y, height, height);
		Point particlePos = new Point(-(x+height/2)*zoomDivider, -(y+height/2)*zoomDivider);
		
		for(int i=0; i<testParticles.size(); i++){
			if(testParticles.get(i) != null){
				testParticles.get(i).paint(particlePos, zoomDivider, g);
			}
			
		}
	}
	
	public void nextFrame() {
		if(!sleeping){
			testParticles.add(new GlowParticle(0, 0));
			//testParticles.add(new Particle(0, 0));
			for(int i=0; i<testParticles.size(); i++){
				testParticles.get(i).nextFrame(testParticles);
			}
		}
	}

	
	
	
	private Color colorFunc(int x){
		double v = (double)x/80;
		if(x/150%2 ==0){
			int r = (int)(Math.sin(v*5)*127)+128%255;
			int g = (int)(Math.sin(v*5+2)*127)+128%255;
			int b = (int)(Math.sin(v*5+4)*127)+128%255;
			return new Color(r, g, b);
		} else{
			int r = (int)Math.abs(Math.sin(v)*255)%255;
			int g = (int)Math.abs(Math.sin(v)*255)%255;
			int b = (int)Math.abs(Math.sin(v)*255)%255;
			return new Color(r, g, b);
		}
	}
	
	private class ImageButton implements Clickable{
		String imageName;
		Rectangle ibb;
		protected ImageButton(String imageName, Rectangle b){
			this.imageName = imageName;
			ibb=b;
		}
		@Override
		public void click(int x, int y) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Rectangle getBounds() {
			return ibb;
		}

		@Override
		public void paint(Graphics g) {
			ImageHandler.getInstance().drawImage(ibb.x, ibb.y, ibb.width, ibb.height, imageName, g);	
		}
		
		
	}
}
